package cn.itcast.web.task;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

//任务类: 等会我的任务目标就是每两秒钟输出一次当前系统时间
public class SendEmailTask {
    @Autowired
    private ContractDao contractDao;

    @Autowired
    private UserService userService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendEmail(){
        ContractExample contractExample = new ContractExample();
        List<Contract> contractList = contractDao.selectByExample(contractExample);
        for (Contract contract : contractList) {
            //查询购销合同的交货期限，判断交货期限是否在3天内，如果在，则发邮件提醒
            Date deliveryPeriod = contract.getDeliveryPeriod();
            long deliveryPeriodTime = deliveryPeriod.getTime();
            long nowTime = new Date().getTime();
            if ((deliveryPeriodTime - nowTime)/1000/60/60/24 <= 3 && (deliveryPeriodTime - nowTime)/1000/60/60/24 >= 0){
                //查出该购销合同的制单人
                String username = contract.getInputBy();
                User user = userService.findByUsername(username);
                //向mq发送消息
                if (user != null){
                    rabbitTemplate.convertAndSend("email_exchange","user.add", user);
                }
            }
        }
    }

}
